package client;

import java.util.ArrayList;
import java.util.List;

import messaage.*;

public class ConnectorWithGui {
	private String sessionID;
	private List<MessagesListener> listeners;
	
	public ConnectorWithGui(){
		this.sessionID = "";
		this.listeners = new ArrayList<MessagesListener>();
	}
	
	public void onData(String data) {
		String[] tmp = data.split("\n");
		if(tmp[0].length() >= 9 && tmp[0].equals("Connected")) {
			String sessionID = tmp[1].split(":")[1];
			this.sessionID = sessionID;
			//inform the listeners that the connection is established
			for (int i = 0; i < this.listeners.size(); i++){
				this.listeners.get(i).connected(new ConnectedMsg(sessionID));
			}
		} else {
			if(tmp.length >= 1 && tmp[0].equals("Disconnected")) {
			//inform the listeners that the connection has been ended successfully.
				for (int i = 0; i < this.listeners.size(); i++){
					this.listeners.get(i).disconnected(new DisconnectedMsg());
				}
			}
			else {
				analyzeMessage(data);
			}
		}
	}

	//this method checks the kind of the given message and informs the listeners with
	//the suitable method for each kind of message
	private void analyzeMessage(String msg) {
		if (msg.startsWith("Error")){
			for (int i = 0; i < this.listeners.size(); i++){
                if (this.listeners.get(i) != null)
                    this.listeners.get(i).errorMessage(new ErrorMsg(msg));
			}
		}
		if (msg.startsWith("Registered")){
			for (int i = 0; i < this.listeners.size(); i++){
                                if (this.listeners.get(i) != null)
                                    this.listeners.get(i).registered(new RegisteredMsg());
                    }
		}
		if (msg.startsWith("Logged In")){
			for (int i = 0; i < this.listeners.size(); i++){
                             if (this.listeners.get(i) != null)
				this.listeners.get(i).loggedIn((new LoggedInMsg(msg)));
                        }
		}
		if (msg.startsWith("LoggedOff")){
			for (int i = 0; i < this.listeners.size(); i++){
                                if (this.listeners.get(i) != null)
				    this.listeners.get(i).loggedOff(new LoggedOffMsg());
                    }
		}
		if (msg.startsWith("AddedForum")){
			for (int i = 0; i < this.listeners.size(); i++){
                            if (this.listeners.get(i) != null)
				this.listeners.get(i).addedForum(new AddedForumMsg(msg));
                        }
		}
		if (msg.startsWith("CreatedThread")){
			for (int i = 0; i < this.listeners.size(); i++){
                            if (this.listeners.get(i) != null)
				this.listeners.get(i).createdThread(new CreatedThreadMsg(msg));
                        }
		}
		if (msg.startsWith("AddedPost")){
			for (int i = 0; i < this.listeners.size(); i++){
                            if (this.listeners.get(i) != null)
				this.listeners.get(i).addedPost(new AddedPostMsg(msg));
                        }
		}
		if (msg.startsWith("FriendAdded")){
			for (int i = 0; i < this.listeners.size(); i++){
                            if (this.listeners.get(i) != null)
				this.listeners.get(i).addedFriend(new AddedFriendMsg(msg));
                        }
		}
		if (msg.startsWith("FriendRemoved")){
			for (int i = 0; i < this.listeners.size(); i++){
                            if (this.listeners.get(i) != null)
				this.listeners.get(i).removedFriend(new RemovedFriendMsg(msg));
                        }
		}
		if (msg.startsWith("EditedPost")){
			for (int i = 0; i < this.listeners.size(); i++){
                            if (this.listeners.get(i) != null)
				this.listeners.get(i).editedPost(new EditedPostMsg(msg));
                        }
		}
                if (msg.startsWith("FriendsListReply")){
                    for (int i = 0; i < this.listeners.size(); i++){
                        if (this.listeners.get(i) != null)
                            this.listeners.get(i).gotFriendsList(new GotFriendsListMsg((msg)));
                    }
                }
                if (msg.startsWith("ForumsListReply")){
                    for (int i = 0; i < this.listeners.size(); i++){
                        if (this.listeners.get(i) != null)
                            this.listeners.get(i).gotListOfForums(new GotListOfForumsMsg(msg));
                    }
                }
                if (msg.startsWith("GotThread")){
                    for (int i = 0; i < this.listeners.size(); i++){
                        if (this.listeners.get(i) != null)
                            this.listeners.get(i).gotThread(new GotThreadMsg((msg)));
                    }
                }
                if (msg.startsWith("GotTopicsOfForum")){
                    for (int i = 0; i < this.listeners.size(); i++){
                        if (this.listeners.get(i) != null)
                            this.listeners.get(i).gotTopicsOfForum(new GotTopicsOfForumMsg((msg)));
                    }
                }
                
                
                
		
	}

	public List<MessagesListener> getListeners() {
		return this.listeners;
	}

	public String getSessionID() {
		return this.sessionID;
	}
	
	public void addListener(MessagesListener tempListener){
		this.listeners.add(tempListener);
	}
	public void removeListener(MessagesListener tempListener){
		this.listeners.remove(tempListener);
	}
}
